package cn.xunjie.ymcc.service.impl;

import cn.xunjie.ymcc.constants.CommonConstants;
import cn.xunjie.ymcc.domain.Login;
import cn.xunjie.ymcc.domain.User;
import cn.xunjie.ymcc.domain.UserBaseInfo;
import cn.xunjie.ymcc.dto.RedisRegisterDto;
import cn.xunjie.ymcc.dto.RegisterDto;
import cn.xunjie.ymcc.feign.LoginFeignClient;
import cn.xunjie.ymcc.mapper.UserMapper;
import cn.xunjie.ymcc.result.JSONResult;
import cn.xunjie.ymcc.service.IUserBaseInfoService;
import cn.xunjie.ymcc.service.IUserService;
import cn.xunjie.ymcc.util.AssertUtil;
import cn.xunjie.ymcc.util.BitStatesUtils;
import cn.xunjie.ymcc.util.StrUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author xue.jiao
 * @since 2022-07-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private LoginFeignClient loginFeignClient;
    @Autowired
    private IUserBaseInfoService userBaseInfoService;



    @Override
    public User loadByPhone(String phone) {
       return userMapper.loadByPhone(phone);
    }

    @Override
    @GlobalTransactional
    public void register(RegisterDto dto) {
        String key = String.format(CommonConstants.REGISTER_PHONE_PRIFIX, dto.getMobile());
        Object o = redisTemplate.opsForValue().get(key);
        AssertUtil.isNotNull(o,"验证码已过期请从新输入");
        RedisRegisterDto redisRegisterDto = (RedisRegisterDto) o;
        AssertUtil.isEquals(redisRegisterDto.getCode(),dto.getSmsCode(),"验证码不正确");
        User user = selectUserByPhone(dto.getMobile());
        AssertUtil.isNull(user,"用户已存在请直接登录");


        JSONResult jsonResult = saveLogin(dto);
        AssertUtil.isTrue(jsonResult.isSuccess(),jsonResult.getMessage());
        AssertUtil.isNotNull(jsonResult.getData(),"信息保存失败");
        Long loginId = Long.valueOf(jsonResult.getData().toString());
        user = saveUser(loginId,dto.getMobile());
        AssertUtil.isNotNull(user.getId(),"用户保存失败");
        //生成邀请码
        String refer = StrUtils.getRandomString(4);
        Long referId = Long.valueOf(refer);
        initUserInfo(user.getId(),dto.getRegChannel(),referId);





    }

    private void initUserInfo(Long id, Integer regChannel,Long referId) {
        UserBaseInfo info = new UserBaseInfo();
        info.setId(id);
        info.setCreateTime(new Date().getTime());
        info.setRegChannel(regChannel);//注册渠道
        info.setReferId(referId);//邀请码id
        userBaseInfoService.insert(info);
    }

    private User saveUser(Long loginId,String mobile) {
        User user = new User();
        user.setLoginId(loginId);
        user.setPhone(mobile);
        user.setNickName(mobile);
        user.setCreateTime(new Date().getTime());
        long stat = BitStatesUtils.addState(BitStatesUtils.OP_REGISTED, BitStatesUtils.OP_ACTIVED);
        long state = BitStatesUtils.addState(stat, BitStatesUtils.OP_AUTHED_PHONE);
        user.setBitState(state);
        insert(user);
        return user;

    }

    private JSONResult saveLogin(RegisterDto dto) {
        Login login = new Login();
        login.setUsername(dto.getMobile());
        login.setPassword(dto.getPassword());
        login.setType(1);
        return loginFeignClient.save(login);

    }

    private User selectUserByPhone(String mobile) {
        EntityWrapper<User> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("phone", mobile);
        return selectOne(entityWrapper);

    }




















}
